package com.xand.bt.market;

import java.util.Date;

import com.xand.bt.api.MarketApiConst;

public class MarketLevel implements Comparable<MarketLevel> {

	private long price;
	
	private long volume;
	
	private byte direction;
	
	private String marketLabel;
	
	private long lastUpdateTime;
	
	public long getLastUpdateTime() {
		return lastUpdateTime;
	}

	public void setLastUpdateTime(long lastUpdateTime) {
		this.lastUpdateTime = lastUpdateTime;
	}

	public long getPrice() {
		return price;
	}

	public void setPrice(long price) {
		this.price = price;
	}

	public long getVolume() {
		return volume;
	}

	public void setVolume(long volume) {
		this.volume = volume;
	}

	public byte getDirection() {
		return direction;
	}

	public void setDirection(byte direction) {
		this.direction = direction;
	}

	public String getMarketLabel() {
		return marketLabel;
	}

	public void setMarketLabel(String marketLabel) {
		this.marketLabel = marketLabel;
	}

	public int compareTo(MarketLevel test) {
		long testPrice = test.getPrice();
		
		int ret = 1;
		
		if(this.direction == MarketApiConst.DIRECTION_BID) {
			
			if(testPrice > this.price)
				ret *= -1;
			
		} else {
			
			if(testPrice < this.price)
				ret *= -1;
			
		}
		
		return ret;
	}
	
	public String toString() {
		
		StringBuilder sb = new StringBuilder();
		
		sb.append("marketLabel=");
		sb.append(marketLabel);
		sb.append("; ");
		
		Date d = new Date(this.lastUpdateTime);
		sb.append("lastUpdateTime=");
		sb.append(MarketApiConst.TIME_ONLY_DATE_FORMAT.format(d));
		sb.append("; ");
		
		sb.append("direction=");
		if(direction == MarketApiConst.DIRECTION_ASK) {
			sb.append("ASK");
		}else{
			sb.append("BID");
		}
		sb.append("; ");
		
		sb.append("price=");
		sb.append(price);
		sb.append("; ");
		
		sb.append("volume=");
		sb.append(volume);
		
		return sb.toString();
	}

	public boolean equals(Object obj) {
		MarketLevel testLevel = (MarketLevel)obj;
		
		return 
				testLevel.getDirection() == this.direction 
				&& testLevel.getMarketLabel().equals(this.marketLabel) 
				&& testLevel.getPrice() == this.price;
	}
	
}
